package com.hcrazy.manchester.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.form.ForumBatchForm;
import com.hcrazy.manchester.app.form.ForumSearchForm;
import com.hcrazy.manchester.app.pojo.Forum;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Topic;
import com.hcrazy.manchester.app.services.ForumService;
import com.hcrazy.manchester.app.services.TopicService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
public class TopicController {
	
	@Autowired
	private TopicService topicService;
	
	@Autowired
	private ForumService forumService;
	
	@RequestMapping("/addTopic")
	public void addTopic(){
		
	}
	
	@RequestMapping("/replyTopic")
	public void replyTopic(){
		
	}
	
	@RequestMapping("/addGroupTopic")
	public void addGroupTopic(){
		
	}
	
	/**
	 * 论坛帖子管理
	 * @param model
	 */
	@RequestMapping("/admin/forum/topic")
	public void topic(Model model){
		//根据查询参数获取分页对象：默认第一页、板块参数为0（全部）、查询字符串为空
		PageModel pageModel = getAllTopics(1, 0, "", "", null, null);
		model.addAttribute("pageModel", pageModel);
				
		//初始化查询对象
		model.addAttribute("searchForm", new ForumSearchForm());
	}
	
	@RequestMapping("/admin/forum/addTopic")
	public String addTopic(Model model) {
		Topic topic = new Topic();
		topic.setAuthor("admin");
		model.addAttribute("topic", topic);
		return "/admin/forum/addTopic";
	}
	
	@RequestMapping("/admin/study/addTopic")
	public void addGroupTopic(Model model) {
		Topic topic = new Topic();
		topic.setAuthor("admin");
		model.addAttribute("topic", topic);
		
	}
	
	@RequestMapping("/admin/forum/topic/{topicId}")
	public String editTopic(Model model,@PathVariable long topicId) {
		Topic topic = null;
		try {
			topic = topicService.getTopicById(topicId);
			model.addAttribute("topic", topic);
			if(topic.getType() == 1){
				return "/admin/study/addTopic";
			}
			return "/admin/forum/addTopic";
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
		
	}
	
	/**
	 * 论坛主题修改
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/topic",method=RequestMethod.POST)
	public String topic(@ModelAttribute("form") Topic topic,Model model){
		try {
			if(topic.getTopicId() > 0){
				topicService.updateTopic(topic);
			}else{
				
				topicService.addTopic(topic);
			}
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		if(topic.getType() == 1){
			return "redirect:/admin/study/topic";
		}
		return "redirect:/admin/forum/topic";
		
	}
	
	
	/**
	 * 群组话题管理
	 * @param model
	 */
	@RequestMapping("/admin/study/topic")
	public void groupTopic(Model model){
		//根据查询参数获取分页对象：默认第一页、板块参数为0（全部）、查询字符串为空
		PageModel pageModel = getGroupTopics(1, 0, "", "", null, null,0);
		model.addAttribute("pageModel", pageModel);
				
		//初始化查询对象
		model.addAttribute("searchForm", new ForumSearchForm());
	}
	
	/**
	 * 论坛主题查询
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/topic/search",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public String search(@ModelAttribute("form") ForumSearchForm search,Model model){
		PageModel pageModel = getAllTopics(search.getPageNum(), search.getForumId(), search.getAuthors(), 
				search.getTitle(), search.getStartTime(), search.getEndTime());
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("searchForm", search);
		return "/admin/forum/topic";
	}
	
	/**
	 * 群组话题查询
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/study/topic/search",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public String searchGroup(@ModelAttribute("form") ForumSearchForm search,Model model){
		if(search.getInfoId() > 0){
			try {
				topicService.delTopic(search.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		PageModel pageModel = getGroupTopics(search.getPageNum(), search.getForumId(), search.getAuthors(), 
				search.getTitle(), search.getStartTime(), search.getEndTime(),search.getSortId());
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("searchForm", search);
		return "/admin/study/topic";
	}
	
	@RequestMapping(value="/admin/forum/topic/batch",method=RequestMethod.POST)
	public String batch(@ModelAttribute("form") ForumBatchForm batchForm,Model model){
		try {
			topicService.batch(batchForm);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return search(batchForm.getSearchForm(),model);
	}
	
	/**
	 * 默认为获取论坛帖子
	 * @param pageNum
	 * @param forumId
	 * @param authors
	 * @param title
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	private PageModel getAllTopics(int pageNum,int forumId,
			String authors,String title,String startTime,String endTime){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return topicService.getTopics(model, forumId, authors, title, startTime, endTime,Constant.TYPE_FORUM,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 获取群组的话题
	 * @param pageNum
	 * @param forumId
	 * @param authors
	 * @param title
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	private PageModel getGroupTopics(int pageNum,int forumId,
			String authors,String title,String startTime,String endTime,int sortId){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return topicService.getTopics(model, forumId, authors, title, startTime, endTime,Constant.TYPE_GROUP,sortId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	@RequestMapping(value="/admin/forum/tree",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<Forum> setForums(){
		try {
			List<Forum> forums = forumService.getAllForums();
			return forums;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}		
	}
}
